# https://blog.csdn.net/weixin_39722759/article/details/111376579
import os

hRecord = {}
lineRecord = []
recordOK = []


def pickup(zuo, you):
    print("此时天平是左倾斜（-1），平衡（0）还是右倾斜（1）？")
    StringCode = input("输入此时状态（可输入状态名或者状态代码）:")
    if StringCode == "左倾斜" or StringCode == "-1":
        return "-1"
    elif StringCode == "平衡" or StringCode == "0":
        return "0"
    elif StringCode == "右倾斜" or StringCode == "1":
        return "1"
    else:
        return "wrong"


def h(nextState):
    h = nextState[0] + nextState[1] + nextState[2] - 1
    # if h == 0 and nextState[4] != 0:
    #     h = 1
    return h


def hJiSuan(nowState):
    left = [0, 0, 0, 0]
    right = [0, 0, 0, 0]

    minGroups = []
    minH = 10000
    for lhs1 in range(nowState[0], -1, -1):
        for ls1 in range(nowState[1], -1, -1):
            for hs1 in range(nowState[2], -1, -1):
                for s1 in range(nowState[3], -1, -1):
                    for lhs2 in range(nowState[0] - lhs1, -1, -1):
                        for ls2 in range(nowState[1] - ls1, -1, -1):
                            for hs2 in range(nowState[2] - hs1, -1, -1):
                                for s2 in range(nowState[3] - s1, -1, -1):
                                    if lhs1 + ls1 + hs1 + s1 == lhs2 + ls2 + hs2 + s2 and lhs1 == 0 and ls1 == 0 and hs1 == 0 and s1 == 0:
                                        continue
                                    if lhs1 + ls1 + hs1 + s1 == lhs2 + ls2 + hs2 + s2 and lhs1 + ls1 + hs1 + s1 <= 6:
                                        left = [lhs1, ls1, hs1, s1]
                                        right = [lhs2, ls2, hs2, s2]
                                        result = left + right
                                        record = nowState + result
                                        zuoh, preZuoState = preZuoResult(nowState, left, right)
                                        youh, preYouState = preYouResult(nowState, left, right)
                                        pingh, prePingState = prePingResult(nowState, left, right)
                                        preh = zuoh + youh + pingh
                                        if preh < 50:
                                            hRecord[record.__str__()] = preh
                                    else:
                                        continue


def preZuoResult(nowState, left, right):
    preState = [0, right[1] + right[0], left[0] + left[2],
                nowState[3] + nowState[1] - right[1] + nowState[2] - left[2] + nowState[0] - (left[0] + right[0]),
                nowState[4] - 1]
    zuoh = h(preState)
    if preState[4] == 0 and not (preState == [0, 1, 0, 11, 0] or preState == [0, 0, 1, 11, 0]):
        zuoh = 1000
    if preState[3] == 12:
        zuoh = 1000
    return zuoh, preState


def preYouResult(nowState, left, right):
    preState = [0, left[1] + left[0], right[0] + right[2],
                nowState[3] + nowState[1] - left[1] + nowState[2] - right[2] + nowState[0] - (left[0] + right[0]),
                nowState[4] - 1]
    youh = h(preState)
    if preState[4] == 0 and  not (preState == [0, 1, 0, 11, 0] or preState == [0, 0, 1, 11, 0]):
        youh = 1000
    if preState[3] == 12:
        youh = 1000
    return youh, preState


def prePingResult(nowState, left, right):
    preState = [nowState[0] - left[0] - right[0], nowState[1] - left[1] - right[1], nowState[2] - left[2] - right[2],
                nowState[3] + left[1] + right[1] + right[2] + left[2] + right[0] + left[0],
                nowState[4] - 1]
    pingh = h(preState)
    if preState[4] == 0 and not (preState == [0, 1, 0, 11, 0] or preState == [0, 0, 1, 11, 0]):
        pingh = 1000
    if preState[3] == 12:
        pingh = 1000
    return pingh, preState


originState = [12, 0, 0, 0, 3]
nowState = [12, 0, 0, 0, 3]
time = 0


# preYouResult(nowState,[3,0,0,0],[3,0,0,0])
def start(nowState):
    hJiSuan(nowState)
    minh = 100
    minKeyOrigin = ""
    if len(hRecord) != 0:
        for key in hRecord:
            if hRecord[key] < minh:
                minh = hRecord[key]
                minKeyOrigin = key
        del hRecord[minKeyOrigin]
        minKey = minKeyOrigin.replace("[", "")
        minKey = minKey.replace("]", "")
        minKey = minKey.split(",")
        isZuo = 0
        isYou = 0
        isPing = 0

        print("---------------------------------------------------------------")
        print("寻找代价函数最小的组合，发现如下称重的代价函数值最小：" + str(minh))
        print("开始第" + str(4 - int(minKey[4])) + "次称重")
        print("原来" + minKey[0] + "个球轻重未知，认为" + minKey[1] + "个球偏轻，" + minKey[2] + "个球偏重，确定" + minKey[3] + "个球为标准重量")
        print("天平左边放" + minKey[5] + "个重量未知的球，" + minKey[6] + "个认为重量偏轻的球，" + minKey[7] + "个认为重量偏重的球，" + minKey[
            8] + "个重量标准的球，")
        print("天平右边放" + minKey[9] + "个重量未知的球，" + minKey[10] + "个认为重量偏轻的球，" + minKey[11] + "个认为重量偏重的球，" + minKey[
            12] + "个重量标准的球，")

        left = [int(minKey[5]), int(minKey[6]), int(minKey[7]), int(minKey[8])]
        right = [int(minKey[9]), int(minKey[10]), int(minKey[11]), int(minKey[12])]
        nowState = [int(minKey[0]), int(minKey[1]), int(minKey[2]), int(minKey[3]), int(minKey[4])]
        print("如果天平向左倾斜")

        zuoh, preZuoState = preZuoResult(nowState, left, right)
        if zuoh == 0 and preZuoState[0] == 0:
            isZuo = 1
            recordpre = preZuoState + left + right + nowState
            recordpre.insert(5, "-1")
            recordOK.append(recordpre)
            lineRecord.append(recordpre)
            print("状态变为：" + str(preZuoState[0]) + "个球的轻重未知，认为" + str(preZuoState[1]) + "个球偏轻，认为" + str(
                preZuoState[2]) + "个球偏重，确定" + str(preZuoState[3]) + "个球为标准重量")
            print("成功得到结果-----------------------------------")
            if preZuoState[1] == 1:
                print("判断在天平右边的球相比重量标准的球偏轻")
            if preZuoState[2] == 1:
                print("判断在天平左边的球相比重量标准的球偏重")

        elif zuoh > 100:
            isZuo = 1
            print("代价函数变为无穷，此次操作后将产生无解，即将尝试其他操作")
        else:
            recordpre = preZuoState + left + right + nowState
            recordpre.insert(5, "-1")
            lineRecord.append(recordpre)
            print("状态变为：" + str(preZuoState[0]) + "个球的轻重未知，认为" + str(preZuoState[1]) + "个球偏轻，认为" + str(
                preZuoState[2]) + "个球偏重，确定" + str(preZuoState[3]) + "个球为标准重量")
            # start(preZuoState)

        print("如果天平向右倾斜")

        youh, preYouState = preYouResult(nowState, left, right)
        if youh == 0 and preYouState[0] == 0:
            isYou = 1

            # lineRecord[preYouState.__str__()] = minKey
            print("状态变为：" + str(preYouState[0]) + "个球的轻重未知，认为" + str(preYouState[1]) + "个球偏轻，认为" + str(
                preYouState[2]) + "个球偏重，确定" + str(preYouState[3]) + "个球为标准重量")
            print("成功得到结果-----------------------------------")
            recordpre = preYouState + left + right + nowState
            recordpre.insert(5, "1")
            lineRecord.append(recordpre)
            recordOK.append(recordpre)
            if preZuoState[1] == 1:
                print("判断在天平左边的球相比重量标准的球偏轻")

            if preZuoState[2] == 1:
                print("判断在天平右边的球相比重量标准的球偏重")

        elif youh > 100:
            isYou = 1
            print("代价函数变为无穷，此次操作后将产生无解，即将尝试其他操作")
        else:
            recordpre = preYouState + left + right + nowState
            recordpre.insert(5, "1")
            lineRecord.append(recordpre)
            print("状态变为：" + str(preYouState[0]) + "个球的轻重未知，认为" + str(preYouState[1]) + "个球偏轻，认为" + str(
                preYouState[2]) + "个球偏重，确定" + str(preYouState[3]) + "个球为标准重量")
            # start(preYouState)

        print("如果天平左右平衡")

        pingh, prePingState = prePingResult(nowState, left, right)
        if pingh == 0 and prePingState[0] == 0:
            isPing = 1
            recordpre = prePingState + left + right + nowState
            recordpre.insert(5, "0")
            lineRecord.append(recordpre)
            recordOK.append(recordpre)
            # lineRecord[prePingState.__str__()] = minKey
            print("状态变为：" + str(prePingState[0]) + "个球的轻重未知，认为" + str(prePingState[1]) + "个球偏轻，认为" + str(
                prePingState[2]) + "个球偏重，确定" + str(prePingState[3]) + "个球为标准重量")
            print("成功得到结果-----------------------------------")

            if preZuoState[1] == 1:
                print("判断未在天平上的球相比重量标准的球偏轻")
            if preZuoState[2] == 1:
                print("判断未在天平上的球相比重量标准的球偏重")

        elif pingh > 100:
            isPing = 1
            print("代价函数变为无穷，此次操作后将产生无解，即将尝试其他操作")
        else:
            recordpre = prePingState + left + right + nowState
            recordpre.insert(5, "0")
            lineRecord.append(recordpre)
            print("状态变为：" + str(prePingState[0]) + "个球的轻重未知，认为" + str(prePingState[1]) + "个球偏轻，认为" + str(
                prePingState[2]) + "个球偏重，确定" + str(prePingState[3]) + "个球为标准重量")

        if isZuo == 0:
            start(preZuoState)
        if isYou == 0:
            start(preYouState)
        if isPing == 0:
            start(prePingState)


def findLine():
    path = "result.txt"

    with open(path, "w", encoding='utf-8') as f:
        f.write("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n")
        f.write("开始打印各种能找到异常重量小球的过程\n")
        f.write("注：统一状态可能有多种情况达到，只显示其中一种，以省略输出\n")

        print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
        print("开始打印各种能找到异常重量小球的过程")
        print("注：统一状态可能有多种情况达到，只显示其中一种，以省略输出")

        global recordOK
        news_ids = []
        for id in recordOK:
            if id not in news_ids:
                news_ids.append(id)
        recordOK = news_ids
        for i in range(recordOK.__len__()):
            f.write("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n")
            f.write("第" + str(i + 1) + "种方案如下：")
            print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
            print("第" + str(i + 1) + "种方案如下：")
            str3 = recordOK[i]

            # recordpre = prePingState + left + right + nowState
            # recordpre.insert(5, "0")

            state3 = [str3[0], str3[1], str3[2], str3[3], str3[4]]
            zuoYouPing = str3[5]
            left = [str3[6], str3[7], str3[8], str3[9]]
            right = [str3[10], str3[11], str3[12], str3[13]]
            state2 = [str3[14], str3[15], str3[16], str3[17], str3[18]]

            result3 = "---------------------------------------------------------------\n" + \
                      "开始第" + str(3 - state3[4]) + "次称重" + "\n" + \
                      "原来" + str(str3[14]) + "个球轻重未知，认为" + str(str3[15]) + "个球偏轻，" + str(str3[16]) + "个球偏重，确定" + str(
                str3[17]) + "个球为标准重量" + "\n" + \
                      "天平左边放" + str(str3[6]) + "个重量未知的球，" + str(str3[7]) + "个认为重量偏轻的球，" + str(
                str3[8]) + "个认为重量偏重的球，" + str(str3[9]) + "个重量标准的球，" + "\n" + \
                      "天平右边放" + str(str3[10]) + "个重量未知的球，" + str(str3[11]) + "个认为重量偏轻的球，" + str(
                str3[12]) + "个认为重量偏重的球，" + \
                      str(str3[13]) + "个重量标准的球，" + "\n"
            if zuoYouPing == "-1":
                result3 = result3 + "天平向左倾斜\n"
            if zuoYouPing == "0":
                result3 = result3 + "天平平衡\n"
            if zuoYouPing == "1":
                result3 = result3 + "天平向右倾斜\n"
            result3 = result3 + "状态变为：" + str(str3[0]) + "个球的轻重未知，认为" + str(
                str3[1]) + "个球偏轻，认为" + str(
                str3[2]) + "个球偏重，确定" + str(str3[3]) + "个球为标准重量\n" + \
                      "成功得到结果-----------------------------------\n"
            if zuoYouPing == "-1":
                if str3[1] == 1:
                    result3 = result3 + "判断在天平右边的球相比重量标准的球偏轻\n"
                if str3[2] == 1:
                    result3 = result3 + "判断在天平左边的球相比重量标准的球偏重\n"
            if zuoYouPing == "0":
                if str3[1] == 1:
                    result3 = result3 + "判断未在天平上的球相比重量标准的球偏轻\n"
                if str3[2] == 1:
                    result3 = result3 + "判断未在天平上的球相比重量标准的球偏重\n"
            if zuoYouPing == "1":
                if str3[1] == 1:
                    result3 = result3 + "判断在天平左边的球相比重量标准的球偏轻\n"
                if str3[2] == 1:
                    result3 = result3 + "判断在天平右边的球相比重量标准的球偏重\n"
            str2 = []
            for i in range(lineRecord.__len__()):
                if state2[0] == lineRecord[i][0] and state2[1] == lineRecord[i][1] and state2[2] == lineRecord[i][2] and \
                        state2[3] == lineRecord[i][3] and \
                        state2[4] == lineRecord[i][4]:
                    str2 = lineRecord[i]
                    #del lineRecord[i]
                    break
            # if str2.__len__() == 0:
            #     f.write(result3)
            #     print(result3)
            #     continue
            zuoYouPing = str2[5]
            left = [str2[6], str2[7], str2[8], str2[9]]
            right = [str2[10], str2[11], str2[12], str2[13]]
            state1 = [str2[14], str2[15], str2[16], str2[17], str2[18]]
            result2 = "---------------------------------------------------------------\n" + \
                      "开始第" + str(3 - state2[4]) + "次称重" + "\n" + \
                      "原来" + str(str2[14]) + "个球轻重未知，认为" + str(str2[15]) + "个球偏轻，" + str(str2[16]) + "个球偏重，确定" + str(
                str2[17]) + "个球为标准重量" + "\n" + \
                      "天平左边放" + str(str2[6]) + "个重量未知的球，" + str(str2[7]) + "个认为重量偏轻的球，" + str(
                str2[8]) + "个认为重量偏重的球，" + str(str2[9]) + "个重量标准的球，" + "\n" + \
                      "天平右边放" + str(str2[10]) + "个重量未知的球，" + str(str2[11]) + "个认为重量偏轻的球，" + str(
                str2[12]) + "个认为重量偏重的球，" + \
                      str(str2[13]) + "个重量标准的球，" + "\n"
            if zuoYouPing == "-1":
                result2 = result2 + "天平向左倾斜\n"
            if zuoYouPing == "0":
                result2 = result2 + "天平平衡\n"
            if zuoYouPing == "1":
                result2 = result2 + "天平向右倾斜\n"
            result2 = result2 + "状态变为：" + str(str2[0]) + "个球的轻重未知，认为" + str(
                str2[1]) + "个球偏轻，认为" + str(
                str2[2]) + "个球偏重，确定" + str(str2[3]) + "个球为标准重量\n"
            if zuoYouPing == "-1":
                if str2[1] == 1:
                    result2 = result2 + "判断在天平右边的球相比重量标准的球偏轻\n"
                if str2[2] == 1:
                    result2 = result2 + "判断在天平左边的球相比重量标准的球偏重\n"
            if zuoYouPing == "0":
                if str2[1] == 1:
                    result2 = result2 + "判断未在天平上的球相比重量标准的球偏轻\n"
                if str2[2] == 1:
                    result2 = result2 + "判断未在天平上的球相比重量标准的球偏重\n"
            if zuoYouPing == "1":
                if str2[1] == 1:
                    result2 = result2 + "判断在天平左边的球相比重量标准的球偏轻\n"
                if str2[2] == 1:
                    result2 = result2 + "判断在天平右边的球相比重量标准的球偏重\n"
            str1 = []
            for i in range(lineRecord.__len__()):
                if state1[0] == lineRecord[i][0] and state1[1] == lineRecord[i][1] and state1[2] == lineRecord[i][2] and \
                        state1[3] == lineRecord[i][3] and \
                        state1[4] == lineRecord[i][4]:
                    str1 = lineRecord[i]
                    #del lineRecord[i]
                    break
            if str1.__len__() == 0:
                # result2=result2.replace("开始第3次称重","开始第2次称重")
                # result3=
                f.write(result2)
                print(result2)
                f.write(result3)
                print(result3)
                continue
            zuoYouPing = str1[5]
            left = [str1[6], str1[7], str1[8], str1[9]]
            right = [str1[10], str1[11], str1[12], str1[13]]
            state0 = [str1[14], str1[15], str1[16], str1[17], str1[18]]
            result1 = "---------------------------------------------------------------\n" + \
                      "开始第" + str(3 - state1[4]) + "次称重" + "\n" + \
                      "原来" + str(str1[14]) + "个球轻重未知，认为" + str(str1[15]) + "个球偏轻，" + str(str1[16]) + "个球偏重，确定" + str(
                str1[17]) + "个球为标准重量" + "\n" + \
                      "天平左边放" + str(str1[6]) + "个重量未知的球，" + str(str1[7]) + "个认为重量偏轻的球，" + str(
                str1[8]) + "个认为重量偏重的球，" + str(str1[9]) + "个重量标准的球，" + "\n" + \
                      "天平右边放" + str(str1[10]) + "个重量未知的球，" + str(str1[11]) + "个认为重量偏轻的球，" + str(
                str1[12]) + "个认为重量偏重的球，" + \
                      str(str1[13]) + "个重量标准的球，" + "\n"
            if zuoYouPing == "-1":
                result1 = result1 + "天平向左倾斜\n"
            if zuoYouPing == "0":
                result1 = result1 + "天平平衡\n"
            if zuoYouPing == "1":
                result1 = result1 + "天平向右倾斜\n"
            result1 = result1 + "状态变为：" + str(str1[0]) + "个球的轻重未知，认为" + str(
                str1[1]) + "个球偏轻，认为" + str(
                str1[2]) + "个球偏重，确定" + str(str1[3]) + "个球为标准重量\n"
            if zuoYouPing == "-1":
                if str1[1] == 1:
                    result1 = result1 + "判断在天平右边的球相比重量标准的球偏轻\n"
                if str1[2] == 1:
                    result1 = result1 + "判断在天平左边的球相比重量标准的球偏重\n"
            if zuoYouPing == "0":
                if str1[1] == 1:
                    result1 = result1 + "判断未在天平上的球相比重量标准的球偏轻\n"
                if str1[2] == 1:
                    result1 = result1 + "判断未在天平上的球相比重量标准的球偏重\n"
            if zuoYouPing == "1":
                if str1[1] == 1:
                    result1 = result1 + "判断在天平左边的球相比重量标准的球偏轻\n"
                if str1[2] == 1:
                    result1 = result1 + "判断在天平右边的球相比重量标准的球偏重\n"
            f.write(result1)
            f.write(result2)
            f.write(result3)
            print(result1)
            print(result2)
            print(result3)


start(nowState)

#news_ids = []
# for id in lineRecord:
#     flag=0
#     for news in news_ids:
#         if id[0]==news[0] and id[1]==news[1] and id[2]==news[2] and id[3]==news[3] and id[4]==news[4]:
#             flag=1
#             break
#     if flag==0:
#         news_ids.append(id)
#lineRecord = news_ids

findLine()
# zuo = [6, 0, 0, 0]
# you = [6, 0, 0, 0]

# while 1 == 1:
#     result = pickup(zuo, you)
#     while result == "wrong":
#         print("输入的内容不符合格式，请重新输入")
#         result = pickup(1, 2)
#
#     if result == "-1":
#         nowState = [0, you[1] + you[0], zuo[0] + zuo[2],
#                     nowState[3] + nowState[1] - you[1] + nowState[2] - zuo[2] + nowState[0] - (zuo[0] + you[0]),
#                     nowState[4] - 1]
#     if result == "0":
#         nowState = [nowState[0] - zuo[0] - you[0], nowState[1] - zuo[1] - you[1], nowState[2] - zuo[2] - you[2],
#                     nowState[3] + zuo[1] + you[1] + you[2] + zuo[2] + you[0] + zuo[0],
#                     nowState[4] - 1]
#     if result == "1":
#         nowState = [0, zuo[1] + zuo[0], you[0] + you[2],
#                     nowState[3] + nowState[1] - zuo[1] + nowState[2] - you[2] + nowState[0] - (zuo[0] + you[0]),
#                     nowState[4] - 1]
#     print(nowState)
